package com.example.module_home.intro

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.core.content.ContextCompat
import androidx.core.view.get
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2
import com.example.module_home.R
import com.example.module_home.login.LoginActivity
import kotlinx.android.synthetic.main.home_activity_intro.*

class IntroActivity : AppCompatActivity(), View.OnClickListener {

    private lateinit var bordList: List<BordItem>
    private lateinit var bordAdapter: BordAdapter
    private var currentPagePosition: Int = 0


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.home_activity_intro)

        initBordIntro()
        setUpIndicator()
        setCurrentIndicate(currentPagePosition)

        tv_skip.setOnClickListener(this)
        btn_start.setOnClickListener(this)
        iv_next.setOnClickListener(this)

    }


    private fun initBordIntro() {
        bordList = arrayListOf(
            BordItem(R.raw.clickable_rainbow, "welcome to use application", "it  a test"),
            BordItem(R.raw.app_update, "did you find something else ?", "yes id do "),
            BordItem(R.raw.lurking_cat, "this is last page", "press skip button to start")
        ).also {
            bordAdapter = BordAdapter(it)
            viewpager2.adapter = bordAdapter
            viewpager2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                override fun onPageSelected(position: Int) {
                    super.onPageSelected(position)
                    currentPagePosition = position
                    setCurrentIndicate(position)
                }
            })

            (viewpager2.getChildAt(0) as RecyclerView).overScrollMode =
                RecyclerView.OVER_SCROLL_NEVER

        }


    }

    private fun setUpIndicator() {
        val indicators = arrayOfNulls<ImageView>(bordAdapter.itemCount)
        val layoutParams = LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT
        )
        layoutParams.setMargins(8, 0, 8, 0)
        for (i in indicators.indices) {
            val imageView = ImageView(applicationContext)
            indicators[i] = imageView
            indicators[i].let {
                it?.setImageDrawable(
                    //ResourcesCompat.getDrawable(resources, R.drawable.indicator_inactive, null)
                    ContextCompat.getDrawable(applicationContext, R.drawable.home_indicator_inactive)
                )
                it?.layoutParams = layoutParams
                indicate_container.addView(it)

            }
        }

    }


    private fun setCurrentIndicate(position: Int) {
        val childCount = indicate_container.childCount
        for (i in 0 until childCount) {
            val imageView = indicate_container[i] as ImageView
            if (i == position) {
                imageView.setImageDrawable(
                    ContextCompat.getDrawable(applicationContext, R.drawable.home_indicator_active)
                )
            } else {
                imageView.setImageDrawable(
                    ContextCompat.getDrawable(applicationContext, R.drawable.home_indicator_inactive)
                )

            }
        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tv_skip -> {
                startActivity(Intent(this, LoginActivity::class.java))

            }
            R.id.btn_start -> {
                startActivity(Intent(this, LoginActivity::class.java))

            }
            R.id.iv_next -> {
                currentPagePosition++
                if (currentPagePosition >= bordAdapter.itemCount ) currentPagePosition = 0
                setCurrentIndicate(currentPagePosition)
                viewpager2.setCurrentItem(currentPagePosition, true)
            }
        }
    }

}